package com.jrapider.manager.controller.role;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrapider.manager.data.om.SysMenu;
import com.jrapider.manager.data.om.SysUser;
import com.jrapider.manager.service.menu.SysMenuService;
import com.jrapider.manager.service.role.SysRoleService;
import com.jrapider.manager.service.user.SysUserService;
import com.jrapider.manager.data.om.SysRole;
import com.jrapider.om.JSONResult;
import com.jrapider.om.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.jrapider.controller.BaseCrudController;

import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/role")
@Api(tags = {"sysRole"})
public class SysRoleController extends BaseCrudController<SysRoleService, SysRole> {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public void buildCondition(QueryWrapper<SysRole> qw, SysRole condition, String filter) {
        if (!StringUtils.isEmpty(filter)) {
            qw.like("name", filter);
        }

    }

    @ApiOperation(value = "查询关联用户", httpMethod = "GET")
    @RequestMapping("/page/user/query")
    public String userQuery(Long roleId, Model model) {
        model.addAttribute("roleId", roleId);
        return "/role/user_query";
    }

    @ApiOperation(value = "查询关联菜单", httpMethod = "GET")
    @RequestMapping("/page/menu/query")
    public String menuQuery(Long roleId, Model model) {
        model.addAttribute("roleId", roleId);
        return "/role/menu_query";
    }

    @ResponseBody
    @ApiOperation(value = "查看用户-针对角色", httpMethod = "POST")
    @RequestMapping("/data/user/load")
    public JSONObject queryUser(Long roleId, String link, String filter, PageInfo pageInfo) {

        IPage<SysUser> pageResult = sysUserService.queryUserWithRole(link, pageInfo.conventToMybatisPage(Object.class), filter, roleId);
        return JSONResult.newBuilder()
                .setRows(pageResult.getRecords())
                .setCount(pageResult.getTotal())
                .success("查询成功")
                .build();

    }

    @ResponseBody
    @ApiOperation(value = "查看菜单-针对角色", httpMethod = "POST")
    @RequestMapping("/data/menu/load")
    public JSONObject queryMenu(Long roleId, String link, String filter, PageInfo pageInfo) {

        IPage<SysMenu> pageResult = sysMenuService.queryMenuWithRole(link, pageInfo.conventToMybatisPage(Object.class), filter, roleId);
        return JSONResult.newBuilder()
                .setRows(pageResult.getRecords())
                .setCount(pageResult.getTotal())
                .success("查询成功")
                .build();

    }

    @ResponseBody
    @ApiOperation(value = "用户绑定角色", httpMethod = "POST")
    @RequestMapping("/data/user/link")
    public JSONObject userLink(Long roleId, Long[] id, Boolean link) {
        if (id == null || id.length == 0) {
            return JSONResult.newBuilder().fail("无数据更新").build();
        }
        return sysUserService.linkUser(roleId, id, link);
    }

    @ResponseBody
    @ApiOperation(value = "菜单绑定角色", httpMethod = "POST")
    @RequestMapping("/data/menu/link")
    public JSONObject menuLink(Long roleId, Long[] id, Boolean link) {
        if (id == null || id.length == 0) {
            return JSONResult.newBuilder().fail("无数据更新").build();
        }
        return sysMenuService.linkMenu(roleId, id, link);
    }
}